<?
class userController extends iMVC_Controller{

    public function index(){
    
        $this->display("user.tpl");
    }
    public function another(){

        //require_once('models/testModel.php'); 
        $tm=new testModel();
        /*
        $tm->id=2;
        $tm->name='bbb';
        $tm->number=888;
        $tm->save();
        */
        $tm->findByPk(28);
        
        //$this->assign('test',$tm->fetchAll('desc'));
        $this->assign('test',$tm);
        $this->display("user.tpl");
    
    }
    public function loginCheck(){
        //<<<
        session_start();
        $name=$_POST["name"];
        $pass=$_POST["pass"];
        $user=new userModel();
        if($user->checkPass($name,$pass)){

            if($user->user_canuse==1){

                $ip=$_SERVER["REMOTE_ADDR"];
                $user->user_ip=$ip;
                $user->save();
                $_SESSION['user']=$user;
                header("Location:".$this->getBaseUrl()."user/center");
            }
            else{
                $this->assign("isSuc",false);
                $this->assign("tip","此账号已被封停");
            }
        }
        else{
                $this->assign("isSuc",false);
                $this->assign("tip","登录失败，请检查用户名和密码!");
        }
        $this->display("user.tpl");
        //>>>
    }

    public function center(){

        $slider=new sliderModel();
        $sRs=$slider->fetchAll();
        $this->assign("slider",$sRs);

        session_start(); 
        $u=$_SESSION['user'];
        $nu=new userModel();
        $nu->user_id=$u->user_id;
        $this->assign("name",$u->user_name);    
        $this->assign("pass",$u->user_pass);    
        $this->assign("isSuc",true);
        $rs=$nu->getInfo();
        $adsRs=$nu->getAds();
        if($rs){
            $table=new iMVC_View_Table();
            $table->setCol('info_ico',new Amxx_View_Table_Pic_Action(),11);
            $table->setCol('info_pic',new Amxx_View_Table_Pic_Action(),11);
            $table->hideCol('info_google_address');
            $table->hideCol('info_city');
            $table->hideCol('info_area');
            $table->hideCol('info_tel');
            $table->hideCol('info_price');
            $table->setCol('info_id',new iMVC_View_Table_Col_Action('预览',$this->getBaseUrl()."info/single","info_id"));
            $table->addCol('删除',new iMVC_View_Table_Col_Action('删除',$this->getBaseUrl()."info/delInfo","info_id"),10);
            $table->addCol('修改',new iMVC_View_Table_Col_Action('修改',$this->getBaseUrl()."info","info_id"),10);
            $table->addCol('修改',new iMVC_View_Table_Col_Action('上传照片',$this->getBaseUrl()."info/uploadPhoto","info_id"),10);
            $table->generateTable($rs);
            $this->assign('table',$table);
        }
        if($adsRs){

            $adsTable=new iMVC_View_Table();
            $adsTable->setCol('ads_pic',new Amxx_View_Table_Pic_Action(),11);
            $adsTable->setCol('预览',new iMVC_View_Table_Col_Action('预览',$this->getBaseUrl()."ads/single","ads_id"));
            $adsTable->generateTable($adsRs);
            $this->assign('adsTable',$adsTable);
        }
        $this->display("user.tpl");

    }
    public function login(){
        //<<<

        $slider=new sliderModel();
        $sRs=$slider->fetchAll();
        $this->assign("slider",$sRs);

        $table=new iMVC_View_Table();
        $user=new userModel();
        
        if(self::isLogin()){   

            session_start();
            unset($_SESSION['user']);
            $this->assign("outTip",'退出成功');
        }
        else{

            $this->assign("outTip",'还未登陆');
        }

            $this->display("login.tpl");
        //>>>
    
    }
    public function regForm(){
    
    
        $this->display("reg.tpl");
    
    }


    //注册
    public function reg(){
    
        $name=$_POST["user_name"];
        $pass=$_POST["user_pass"];
        $cpass=$_POST["user_cpass"];
        $email=$_POST["user_email"];
        $error=array();
        $user=new userModel(); 

        if($user->checkNameExist($name)){
            $error[]='用户名已存在';
        }
        $vt=new iMVC_Validator($this->post);
        $vt->addRule('user_name','{"max-length":20,"min-length":3}','{"requried":"必须填写名称","max-length":"用户名不能超过20个字符","min-length":"用户名不能小于6个字符"}');
        $vt->addRule('user_name','{"requried":true}','{"equalTo":"需要等于123456"}');
        $vt->addRule('user_email','{"email":true}','{"email":"请输入正确的email格式"}');
        $vt->addRule('user_pass','{"requried":true,"max-length":20,"min-length":3,"equalTo":"'.$cpass.'"}','{"requried":"必须填写密码","max-length":"不能超过20个字符","min-length":"密码不能小于6个字符","equalTo":"两次密码需一致"}');
        $vt->validate();
        $error=array_merge($error,$vt->getErrorList());

        if(count($error)!=0){
            $this->assign("errors",$error);
            $this->assign("isSuc",false);
            $this->assign("regTips","注册失败");
        }
        else{
            $user->user_name=$name;
            $user->user_pass=$pass;
            $user->user_email=$email;
            $user->save();
            $this->assign("isSuc",true);
            $this->assign("regTips","注册成功");

        }
            $this->display("error.tpl");
    
    }

    
    public function del(){
    
        $id=$_GET['id']; 
        $user=new userModel();
        $user->user_id=$id;
        $user->del();
        header("location:".$this->getBaseUrl()."user/login");
    }   
    public function forbit(){

        $id=$_GET['id']; 
        $user=new userModel();

        if($user->findByPk($id)){
            if($user->user_canuse==1){

                $user->user_canuse=0;
            }
            else{
                $user->user_canuse=1;
            }
            $user->save();
        }
        header("location:".$this->getBaseUrl()."admin/user");
    }
    public function forbitIp(){
        $id=$_GET["id"];
        if(!empty($id)){
            $user=new userModel();
            $user->findByPk($id);
            $ip_address=$user->user_ip;
            $ip=new ipModel();
            $ip->ip_address=$ip_address;
            $ip->save();
        }
    
    
    
    }
    public function loginOut(){
    
        session_start();
        unset($_SESSION['user']); 
        header("Location:".$this->getBaseUrl());
    
    }

    //验证是否登录
    public static function isLogin($redirect=false){
        //<<<
        session_start();
        if(!isset($_SESSION['user'])){
        
            if($redirect){
                $base=parent::getStaticBaseUrl();
                header("Location:".$base."user/login");
            }
            return false;  
        } 
        else{
            return true; 
        
        }
        //>>>
    }
    

}
